import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { ReuseTabService } from '@delon/abc';
import { DatePipe } from '@angular/common';
import { _HttpClient } from '@delon/theme';
import { TableCheckPageComponent } from '../../../layout/components/tableCheckPage.component';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-caddrawings',
    templateUrl: './cadDrawings.component.html',
    styleUrls: ['./cadDrawings.component.less']
})

export class CadDrawingsComponent implements OnInit {
    @ViewChild('tcp', { static: true }) tcp: TableCheckPageComponent;
    treeNodeStyle = { 'max-width': '300px', 'max-height': '400px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };

    searchParam: any = {};
    pageType = '';
    queryUrl = environment.project + '/cad/query';
    paramStatus = '';
    headData = [];
    plainData = [];
    xbSelect = [];
    czSelect = [];
    tcmcSelect = [
        { value: '0', label: '车站联锁图册' },
        { value: '1', label: '自动闭塞图册' },
        { value: '2', label: '信号集中监测图册' },
        { value: '3', label: '集中监测图册' },
        { value: '4', label: 'CTC(TDCS)图册' },
        { value: '5', label: '组合内部配线图册' },
        { value: '6', label: '室外箱盒配线图册' },
        { value: '7', label: '电源屏技术图纸' }
    ];
    tzmcSelect = [
        { value: '0', label: '技术图' },
        { value: '1', label: '配线图' }
    ];
    cadData: any = {};
    scbmNodes = [];
    operaList: any = [
        { addModal: false },
        { editModal: false },
        { delModal: false }
    ];
    tempData = [];
    apiUrl: any = {};
    compareFn = (o1: any, o2: any) => (o1 && o2 ? o1.id === o2.id : o1 === o2);

    constructor(
        private activatedRoute: ActivatedRoute,
        private datePipe: DatePipe,
        private reuseTabService: ReuseTabService,
        private http: _HttpClient
    ) { }

    ngOnInit() {
        this.activatedRoute.params.subscribe((params: Params) => {
            this.pageType = params.type;
            switch (this.pageType) {
                case 'view':
                    this.reuseTabService.title = 'CAD图纸查看';
                    this.paramStatus += '&readOnly=1';
                    break;
                case 'edit':
                    this.reuseTabService.title = 'CAD图纸维护';
                    this.paramStatus += '&readOnly=0';
                    break;
            }
        });

        this.headData = [
            { "name": "线别", "width": "8%" },
            { "name": "车站", "width": "10%" },
            { "name": "图册名称", "width": "10%" },
            { "name": "图纸名称", "width": "8%" },
            { "name": "CAD图纸名称", "width": "15%" },
            { "name": "上传时间", "width": "10%" },
            { "name": "上传人员", "width": "8%" },
            { "name": "上传部门", "width": "10%" },
            { "name": "备注", "width": "" }
        ];
        this.plainData = ["line", "stationName", "album", "dwg", "cadDrawingsFiles", "createTime", "createUser", "createOrgName", "remark"];
        this.cadData = { 'tcmcSelect': this.tcmcSelect, 'tzmcSelect': this.tzmcSelect };
        this.http.get(environment.project + '/getAllLine').subscribe((res: any) => {
            if (!res.code) {
                this.xbSelect = res.data;
            }
        });
        this.http.get(environment.apiBase + '/csmis/getKmnOrgTree.jwt?showAll=1&showSeg=1').subscribe((res: any) => {
            this.scbmNodes = res;
        });
        this.apiUrl.add = environment.project + '/cad/add';
        this.apiUrl.edit = environment.project + '/cad/update';
        this.apiUrl.del = environment.project + '/cad/delete';
        this.loadList();
    }
    loadList() {
        this.tcp.loadList(this.queryUrl, this.paramStatus);
    }
    search() {
        let params = this.paramStatus;
        for (const item of Object.keys(this.searchParam)) {
            const data = this.searchParam[item];
            if (data || data === 0) {
                if (item === 'line') {
                    params += '&line=' + this.searchParam.line.name;
                } else {
                    params += '&' + item + '=';
                    if (data instanceof Date) {
                        params += this.datePipe.transform(data, 'yyyy-MM-dd');
                    } else {
                        params += data;
                    }
                }
            }
        }
        this.tcp.loadList(this.queryUrl, params);
    }
    lbChange(e) {
        if (e) {
            this.czSelect = [];
            this.http.get(environment.project + '/getStationByLine?line=' + e.id).subscribe((res: any) => {
                if (!res.code) {
                    this.czSelect = res.data;
                }
            });
        }
    }
    modalShow(type) {
        this.tempData = this.tcp.getAllData();
        this.operaList[type] = true;
    }
    modalHide(type) {
        this.operaList[type] = false;
    }
}
